package tijava.io;

//: io/BufferedInputFile.java
import java.io.*;

import net.mindview.util.TextFile;

public class BufferedInputFile {
	private static String cp = "/tijava/io/test.file";

	public static String readFileByLine(String filename) throws IOException {
		// Reading input by lines:
		BufferedReader in = new BufferedReader(new FileReader(filename));
		String s;
		StringBuilder sb = new StringBuilder();
		while ((s = in.readLine()) != null)
			sb.append(s + "\n");
		in.close();
		return sb.toString();
	}

	public static void formattedMemoryInput(String str) throws IOException {
		DataInputStream in = new DataInputStream(new ByteArrayInputStream(
				str.getBytes()));
		while (in.available() != 0) {
			System.out.print((char) in.readByte());
		}
	}

	public static void orignalFileWriter(String str) throws IOException {
		String fileName = "file.out";
		PrintWriter w = new PrintWriter(new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream(fileName))));
		w.print("orignalFileWriter:\r\n" + str);
		w.close();
		System.out.print(readFileByLine(fileName));
	}

	public static void shotcutFileWriter(String str) throws IOException {
		String fileName = "./bin/tijava/io/file.out";
		PrintWriter w = new PrintWriter(fileName);
		w.print("shotcutFileWriter:\r\n" + str);
		w.close();
		System.out
				.print(readFileByLine(TextFile.getFilePathUseClasspath(fileName
						.substring("./bin".length()))));
	}

	public static void main(String[] args) throws IOException {
		System.out.print(readFileByLine(TextFile.getFilePathUseClasspath(cp)));
		formattedMemoryInput(readFileByLine(TextFile
				.getFilePathUseClasspath(cp)));

		orignalFileWriter(readFileByLine(TextFile.getFilePathUseClasspath(cp)));
		shotcutFileWriter(readFileByLine(TextFile.getFilePathUseClasspath(cp)));
	}
} /* (Execute to see output) */// :~
